package com.harris.RotateTest2;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;

public class RotateTest2 extends Activity {
    /** Called when the activity is first created. */
	private ImageView mImageView1, mImageView2;
	private float mCenterX,mCenterY;
	private boolean isView1Visiable;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mImageView1=(ImageView)findViewById(R.id.image1);
        mImageView2=(ImageView)findViewById(R.id.image2);
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	// TODO Auto-generated method stub
    	doClockwiseRotate();
    	return super.onTouchEvent(event);
    }
    
    private void doClockwiseRotate(){
    	if(mImageView1.getVisibility() == View.VISIBLE){
    		mCenterX = mImageView1.getWidth()/2.0f;
    		mCenterY = mImageView1.getHeight()/2.0f;
    	}
    	if(mImageView2.getVisibility() == View.VISIBLE){
    		mCenterX = mImageView2.getWidth()/2.0f;
    		mCenterY = mImageView2.getHeight()/2.0f;
    	}
    	
    	Rotate3d leftAnimation = new Rotate3d(-0, -90, 0, 0, mCenterX, mCenterY);
        Rotate3d rightAnimation = new Rotate3d(-0+90, -90+90, 0.0f, 0.0f, mCenterX, mCenterY);
        
        leftAnimation.setFillAfter(true);
        leftAnimation.setDuration(1000);
        rightAnimation.setFillAfter(true);
        rightAnimation.setDuration(1000);
        if(mImageView1.getVisibility() == View.VISIBLE){
        	mImageView1.startAnimation(leftAnimation);
        	mImageView2.startAnimation(rightAnimation);
       // 	mImageView1.setAnimation(leftAnimation);
       // 	mImageView2.setAnimation(rightAnimation);
        	mImageView2.setVisibility(View.VISIBLE);
        	isView1Visiable = true;
        }else{
        	isView1Visiable = false;
        	mImageView2.startAnimation(leftAnimation);
        	mImageView1.startAnimation(rightAnimation);
        //	mImageView2.setAnimation(leftAnimation);
        //	mImageView1.startAnimation(rightAnimation);
        	mImageView1.setVisibility(View.VISIBLE);
        }
        leftAnimation.setAnimationListener(new AnimationListener(){
        	@Override
        	public void onAnimationEnd(Animation animation) {
        		// TODO Auto-generated method stub
        		if(isView1Visiable){
        			mImageView1.setVisibility(View.GONE);
        			mImageView2.setVisibility(View.VISIBLE);
        		}else{
        			mImageView2.setVisibility(View.GONE);
        			mImageView1.setVisibility(View.VISIBLE);
        		}
        	
        	}
        	@Override
        	public void onAnimationRepeat(Animation animation) {
        		// TODO Auto-generated method stub
        		
        	}
        	@Override
        	public void onAnimationStart(Animation animation) {
        		// TODO Auto-generated method stub
        		
        	}
        });
        rightAnimation.setAnimationListener(new AnimationListener(){
        	@Override
        	public void onAnimationEnd(Animation animation) {
        		// TODO Auto-generated method stub
        		
        	}
        	@Override
        	public void onAnimationRepeat(Animation animation) {
        		// TODO Auto-generated method stub
        		
        	}
        	@Override
        	public void onAnimationStart(Animation animation) {
        		// TODO Auto-generated method stub
        		
        	}
        });
    }
}